<template>
  <n-modal
    :mask-closable="false"
    :closable="false"
    :close-on-esc="false"
    preset="card"
    :title="entityDialogueStore.dialogCategory"
    style="width: 1000px"
  >
    <n-form
      label-width="80"
      ref="editFormRef"
      label-placement="left"
      label-align="left"
      :model="editForm"
      require-mark-placement="left"
      :rules="Rules"
    >
      <n-grid :cols="24" x-gap="12" y-gap="8">
        <n-gi :span="6">
          <n-form-item label="编码：" path="baseNo" label-width="80">
            <n-input v-model:value="editForm.baseNo" placeholder="请输入编码">
            </n-input>
          </n-form-item>
        </n-gi>
        <n-gi :span="6">
          <n-form-item label="名称：" path="name" label-width="80">
            <n-input v-model:value="editForm.name" placeholder="请输入名称">
            </n-input>
          </n-form-item>
        </n-gi>
      </n-grid>
    </n-form>

    <template #footer>
      <div style="margin: 0 auto; width: 200px">
        <n-button class="footer-btn" type="primary" @click="confirm()"
          >确认
        </n-button>
        <n-button class="footer-btn" @click="cancel()"> 取消</n-button>
      </div>
    </template>
  </n-modal>
</template>

<script lang="ts" setup>
import { getEntityStore } from "@/handle/views/base/GetEntityStore";
import { getBaseApi } from "@/handle/views/base/GetBaseApi";
import { getEntity } from "@/handle/views/base/GetEntity";

const props = defineProps<{
  entityName: string; //实体类名
}>();
const entityDialogueStore = getEntityStore(props.entityName);
const entityApi = getBaseApi(props.entityName);
const curEntity = getEntity(props.entityName);

const editForm = ref(curEntity);
const editFormRef = ref();

// 订阅 .
entityDialogueStore.$subscribe((mutation: any, state: any) => {
  editForm.value = state.selectedRow;
});

const message = useMessage();

//确认按钮
const confirm = () => {
  switch (entityDialogueStore.dialogCategory) {
    case "编辑": {
      editFormRef.value.validate((error: boolean) => {
        if (!error) {
          if (editForm.value.id) {
            entityApi.editEntity(editForm.value.id, editForm.value).then(() => {
              editForm.value = curEntity;
              entityDialogueStore.setEntityList(curEntity);
              entityDialogueStore.closeDialog();
            });
          } else {
            message.error("没有id!");
          }
        }
      });
      break;
    }
    case "新增": {
      editFormRef.value.validate((error: boolean) => {
        if (!error) {
          entityApi.insertEntity(editForm.value).then(() => {
            editForm.value = curEntity;
            entityDialogueStore.setEntityList(curEntity);
            entityDialogueStore.closeDialog();
          });
        }
      });
      break;
    }
    case "查询": {
      entityDialogueStore
        .setEntityList(editForm.value)
        .then(() => {
          entityDialogueStore.closeDialog();
          editForm.value = curEntity;
          message.success("查询成功!");
        })
        .catch((err: any) => {
          message.error("系统错误, 请重试!");
          console.log(err);
        });
      break;
    }
  }
};

//取消按钮
const cancel = () => {
  entityDialogueStore.closeDialog();
};

//校验
const Rules = {
  baseNo: {
    required: true,
    message: "请输入编码",
    trigger: "blur",
  },
  name: {
    required: true,
    message: "请输入名称",
    trigger: "blur",
  },
};
</script>

<style scoped></style>
